package com.focus.pay.ali.action.order;

import cn.hutool.json.JSONUtil;
import com.alipay.api.AlipayClient;
import com.alipay.api.internal.util.WebUtils;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.focus.pay.ali.config.AliPayConfig;
import com.focus.pay.ali.model.close.AliPayModelClose;
import com.focus.pay.ali.model.download.AliPayModelTradeDownload;
import com.focus.pay.ali.model.refund.AliPayModelRefund;
import com.focus.pay.ali.constant.Constant;
import com.focus.pay.ali.model.order.AliPayModelQuery;
import com.focus.pay.ali.model.refund.AliPayModelRefundQuery;
import com.focus.pay.core.exception.ExceptionEnum;
import com.focus.pay.core.exception.PayException;

/**
* @Author: 赵参谋
* @Date: 2021/9/28 22:25
* @Methods:
 * 1:支付结果查询
 * 2:支付交易关闭
 * 3:支付退款申请
 * 4:支付退款查询
 * 5:支付账单下载
 * 参考文档<link>https://opendocs.alipay.com/apis</link>
*/
public class AliPayOrder{

    /**
     * 支付结果查询
     * @param aliPayModelQuery 支付宝查询参数
     * @param aliPayConfig 支付宝支付参数
     */
    public AlipayTradeQueryResponse payQuery(AliPayModelQuery aliPayModelQuery, AliPayConfig aliPayConfig) throws PayException {
        try{
            AlipayClient alipayClient = Constant.buildAlipayClient(aliPayConfig);
            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            WebUtils.setNeedCheckServerTrusted(false);
            request.setBizContent(JSONUtil.toJsonStr(aliPayModelQuery));
            return  alipayClient.execute(request);
        }catch (Exception e){
            throw new PayException(ExceptionEnum.ORDER_EXCEPTION);
        }
    }

    /**
     * 支付交易关闭
     * @param aliPayModelClose 支付取消参数
     * @param aliPayConfig 支付宝支付参数
     * @throws PayException
     */
    public AlipayTradeCloseResponse  payClose(AliPayModelClose aliPayModelClose, AliPayConfig aliPayConfig) throws PayException {
        try{
            AlipayClient alipayClient = Constant.buildAlipayClient(aliPayConfig);
            AlipayTradeCloseRequest   request = new AlipayTradeCloseRequest  ();
            WebUtils.setNeedCheckServerTrusted(false);
            request.setBizContent(JSONUtil.toJsonStr(aliPayModelClose));
            return  alipayClient.execute(request);
        }catch (Exception e){
            throw new PayException(ExceptionEnum.ORDER_EXCEPTION);
        }
    }

    /**
     * 支付退款
     * @param aliPayModelRefund 支付宝退款参数
     * @param aliPayConfig 支付宝支付参数
     * @throws PayException
     */
    public AlipayTradeRefundResponse payRefund(AliPayModelRefund aliPayModelRefund, AliPayConfig aliPayConfig) throws PayException {
        try{
            AlipayClient alipayClient = Constant.buildAlipayClient(aliPayConfig);
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest  ();
            WebUtils.setNeedCheckServerTrusted(false);
            request.setBizContent(JSONUtil.toJsonStr(aliPayModelRefund));
            return  alipayClient.execute(request);
        }catch (Exception e){
            throw new PayException(ExceptionEnum.ORDER_EXCEPTION);
        }
    }

    /**
     * 支付退款查询
     * @param aliPayModelRefundQuery 支付宝退款查询参数
     * @param aliPayConfig 支付宝支付参数
     * @throws PayException
     */
    public AlipayTradeFastpayRefundQueryResponse payRefundQuery(AliPayModelRefundQuery aliPayModelRefundQuery, AliPayConfig aliPayConfig) throws PayException {
        try{
            AlipayClient alipayClient = Constant.buildAlipayClient(aliPayConfig);
            AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
            WebUtils.setNeedCheckServerTrusted(false);
            request.setBizContent(JSONUtil.toJsonStr(aliPayModelRefundQuery));
            return  alipayClient.execute(request);
        }catch (Exception e){
            throw new PayException(ExceptionEnum.ORDER_EXCEPTION);
        }
    }

    /**
     * 对账单下载
     * @param tradeDownload 对账参数
     * @param aliPayConfig 支付宝支付参数
     * @throws PayException
     * 业务方根据需求处理下载URL
     */
    public AlipayDataDataserviceBillDownloadurlQueryResponse tradeDownload(AliPayModelTradeDownload tradeDownload, AliPayConfig aliPayConfig) throws PayException {
        try{
            AlipayClient alipayClient = Constant.buildAlipayClient(aliPayConfig);
            AlipayDataDataserviceBillDownloadurlQueryRequest  request = new AlipayDataDataserviceBillDownloadurlQueryRequest ();
            WebUtils.setNeedCheckServerTrusted(false);
            request.setBizContent(JSONUtil.toJsonStr(tradeDownload));
            return  alipayClient.execute(request);
        }catch (Exception e){
            throw new PayException(ExceptionEnum.ORDER_EXCEPTION);
        }
    }
}
